set mem 1g
set matsize 800
set more off
prog drop _all
capture log close
clear

pause off

capture log close



global dir_data_out     "out_datasmm_new" 


global init_age = 15


*************************************************************************************
*** non-targeted variantions in smoking take-up between enrolled and not-enrolled by age

use NLSY97_PublicData_FinalSample, clear

capture matrix drop reg_out

local minage 18 
local maxage 24

foreach age of numlist `minage'/`maxage' {

	reg dq enroll if addiction == 0 & age == `age' 
	
matrix dq0_b    = get(_b)'
matrix dq0_bcov = get(VCE) 
matrix dq0_bvar = vecdiag(dq0_bcov)' 

matmap dq0_bvar dq0_bse, map(sqrt(@))

matrix list dq0_b 
matrix list dq0_bse 

matrix reg_out      = [ nullmat(reg_out)   \ `age', dq0_b[1,1], dq0_bse[1,1] ]  

} 

matrix list reg_out


preserve 
clear
svmat reg_out

rename reg_out1 age
rename reg_out2 coef_dq0
rename reg_out3 se_dq0

gen data = 1 

saveold $dir_data_out/nontargeted_gap_dq0_byAge, replace v(13)

restore 


*************************************************************************************
*** non-targeted variantions: age 30 smoking rate and addiction stock 

use NLSY97_PublicData_FinalSample, clear

xtset id age 

gen dq_a30_ = dq if age == 30
by id: egen dq_a30 = max(dq_a30_)
drop dq_a30_

gen addiction_cat = .
replace addiction_cat = 1 if addiction == 1
replace addiction_cat = 2 if addiction == 2 | addiction == 3
replace addiction_cat = 3 if addiction == 4 | addiction == 5
replace addiction_cat = 4 if addiction > 5 & addiction != . 

	
	tabstat dq_a30 if dq == 0 & age <= 29 & age >= 25, by(addiction_cat) stat(mean) columns(statistics) save	
	matrix vec_mean_c = [ r(Stat1), r(Stat2), r(Stat3), r(Stat4)]
	
	
	tabstat dq_a30 if dq == 0 & age <= 29 & age >= 25, by(addiction_cat) stat(semean) columns(statistics) save	
	matrix vec_se_c = [ r(Stat1), r(Stat2), r(Stat3), r(Stat4)]
	
	matrix vec_mean = [vec_mean_c   ]
	matrix vec_se   = [vec_se_c     ]	
	matrix vec =      [vec_mean_c, vec_se_c ]

	matrix list vec
		
	preserve 

	
	clear
	svmat vec_mean
	
	outsheet using "$dir_data_out/nontargeted_relapse_mean.txt",nonames noquote nolabel replace
	
	matrix drop vec_mean_c 
	
	
	clear
	svmat vec_se
		
	outsheet using "$dir_data_out/nontargeted_relapse_semean.txt", nonames noquote nolabel replace
	
	matrix drop vec_se_c
	
		
	clear
	svmat vec
		
	rename vec1     dq_add1
	rename vec2     dq_add2
	rename vec3     dq_add3
	rename vec4     dq_add4
	rename vec5     se_add1
	rename vec6     se_add2
	rename vec7     se_add3
	rename vec8     se_add4

	gen data = 1 
	
	reshape long dq_add se_add, i(data) j(addcat)
	
	saveold $dir_data_out/nontargeted_smokingrelapse, replace v(13)


restore 



capture log close


